package remote{
	
	import entidades.Cotacao;
	import entidades.SolicitacaoCotacao;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class CotacaoRemote {								
		
		public function recuperarSolEdit( result:Function ):void{
			var async:AsyncToken = _remote.recuperarSolEdit();
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function recuperarCot( result:Function ):void{
			var async:AsyncToken = _remote.recuperarCot();
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function recuperarItens( result:Function, sol:SolicitacaoCotacao  ):void{
			var async:AsyncToken = _remote.recuperarItens(sol);
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function buscaCotacoes( result:Function ):void{
			var async:AsyncToken = _remote.buscaCotacoes();
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function recuperarSol( result:Function ):void{
			var async:AsyncToken = _remote.recuperarSol();
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function editarCot( result:Function, cotacao:Cotacao):void{
			var async:AsyncToken = _remote.gravarCotacaoSessao(cotacao);
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function criarCotacao( result:Function, solic:SolicitacaoCotacao):void{
			var async:AsyncToken = _remote.gravarSolicParaCotar(solic);
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		public function exibirCotacao( result:Function, cotacao:Cotacao ):void{
			var async:AsyncToken = _remote.gravarCotacaoSessao(cotacao);
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		} 
		
		public function gravarCotacao( result:Function, cot:Cotacao, status:Number, sol:SolicitacaoCotacao, numCot:Number ):void{
			var async:AsyncToken = _remote.salvarCotacao(cot, status, sol, numCot);
			async.addResponder( new Responder( result, defaultFaultHandler ) );			
		}
		
		public function editarCotacao( result:Function, cot:Cotacao, fornecedores:Array ):void{
			var async:AsyncToken = _remote.editarCotacao(cot, fornecedores);
			async.addResponder( new Responder( result, defaultFaultHandler ) );			
		}
		
		public function buscarCotacao(result:Function, numCotacao:Number, numRequisicao:Number, fornecedor:Number,
										 idStatus:Number, tipo:String, dtFimDe:Date, dtFimAte:Date):void{		
			var result:Function;
			var async:AsyncToken = _remote.buscarCotacao(numCotacao, numRequisicao, fornecedor,
				idStatus, tipo, dtFimDe, dtFimAte);
			async.addResponder( new Responder( result, defaultFaultHandler ) );
		}
		
		//--------------------setup----------------------------//
		private var _remote:RemoteObject;
		
		public function CotacaoRemote(){
			_remote = new RemoteObject();
			_remote.showBusyCursor = true;
			_remote.destination = 'CotacaoService';
		}
		
		private function defaultFaultHandler(e:FaultEvent):void{
			Alert.show("Estamos com problemas no servidor! Tente novamente mais tarde!","Cotação");
		}
		
		private static var _instance:CotacaoRemote;
		public static function getInstance():CotacaoRemote{
			if(_instance == null){
				_instance = new CotacaoRemote();
			}
			return _instance;
		}
	}
}